<template>
  <el-dialog v-model="show" :title="title" width="1000px" append-to-body>
    <el-form :inline="true" :model="query" style="display: flex;">
      <el-form-item label="姓名">
        <el-input v-model="query.name" style="width: 120px;" clearable />
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="query.sex" placeholder="请选择" style="width: 100px;" clearable @change="getList">
          <el-option label="全部" value="" />
          <el-option label="男" value="1" />
          <el-option label="女" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期">
        <el-date-picker
          v-model="birthArr" clearable
          format="YYYY-MM-DD" value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="getList"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList">查询</el-button>
      </el-form-item>
    </el-form>
    <p v-if="noPhotoCanSign == 0" class="text-danger">*需上传照片才可报名</p>
    <el-table ref="allSportmenTable" v-loading="loading" :data="tableData" height="60vh" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" label="选择" :selectable="selectable" />
      <el-table-column label="照片" width="70">
        <template #default="scope">
          <img :src="scope.row.picUrl" style="width: 50px">
        </template>
      </el-table-column>
      <el-table-column prop="realName" label="姓名" width="120" />
      <el-table-column prop="sex" label="性别" width="70">
        <template #default="scope">
          <span v-if="scope.row.sex=='0'">女</span>
          <span v-if="scope.row.sex=='1'">男</span>
        </template>
      </el-table-column>
      <el-table-column label="证件类型" width="80">
        <template #default="scope">
          <span v-if="scope.row.idcType=='0'">身份证  </span>
          <span v-if="scope.row.idcType=='1'">护照  </span>
          <span v-if="scope.row.idcType=='2'">其他  </span>
        </template>
      </el-table-column>
      <el-table-column prop="idcCode" label="证件号码" width="200" />
      <el-table-column label="出生日期" width="120">
        <template #default="scope">
          <span>{{ scope.row.birth.substring(0,10) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="联系方式" />

      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button type="text" @click="editPerson(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <template #footer>
      <div class="dialog-footer text-center">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>

  <dialogPersonInfo ref="editPersonInfoRef" @submitForm="getList" />

</template>

<script setup>
import { reactive, ref, toRefs, watch } from 'vue'
import { getCurrentInstance, nextTick, onMounted } from '@vue/runtime-core'
import * as match from '@/api/match/pc'
import dialogPersonInfo from '../components/addCoach'

const { proxy } = getCurrentInstance()
const emit = defineEmits(['submitForm', 'transfer'])
const data = reactive({
  query: {},
  tableData: [],
  birthArr: [],
  show: false,
  loading: false,
  title: '选择运动员',
  noPhotoCanSign: 0
})
const { query, tableData, show, title, birthArr, loading, noPhotoCanSign } = toRefs(data)
let matchId
let groupId
let choosedList = []
const choosedIds = []
const open = (params) => {
  matchId = params.matchId
  groupId = params.groupId
  noPhotoCanSign.value = params.noPhotoCanSign
  show.value = true
  choosedList = params.choosedList
  for (const p of choosedList) {
    choosedIds.push(p.id)
  }
  getList()
}
defineExpose({
  open
})
const selectable = (row) => {
  if (!row.picUrl && noPhotoCanSign.value == 0) {
    return false
  } else {
    return true
  }
}
const getList = () => {
  loading.value = true
  if (birthArr.value) {
    query.value.birthBegin = birthArr.value[0]
    query.value.birthEnd = birthArr.value[1]
  }
  match.getMyGroupForCptFilter(groupId, matchId, query.value).then(res => {
    tableData.value = res.data.athletes
    nextTick(() => {
      for (const n in tableData.value) {
        if (choosedIds.indexOf(tableData.value[n].id) > -1) {
          proxy.$refs['allSportmenTable'].toggleRowSelection(tableData.value[n], true)
        }
      }
    })
    loading.value = false
  })
}

function handleSelectionChange(val) {
  // console.log(val)
  choosedList = val
}
function submitForm() {
  emit('transfer', choosedList)
  // emit('submitForm', choosedList)
  show.value = false
}
function cancel() {
  show.value = false
}
function editPerson(row) {
  const params = {
    id: row.id,
    groupId: groupId
  }
  proxy.$refs['editPersonInfoRef'].open(params)
}

</script>

<style scoped>

</style>
